package com.fanrui.code.game;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.ibatis.session.SqlSession;
import com.fanrui.code.entity.User;
import com.fanrui.code.entity.UserExample;
import com.fanrui.code.entity.UserMapper;
import com.fanrui.code.thread.NamedThreadFactory;
import com.fanrui.code.util.SessionUtil;
import com.fanrui.code.util.core.DateUtil;

public class SignManager {
	// singleton
	private SignManager() {
	}

	private static class ManagerSingleton {
		private static SignManager instance = new SignManager();
	}

	public static SignManager getInstance() {
		return ManagerSingleton.instance;
	}

	private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(3,
			new NamedThreadFactory("SignReset-"));

	public void startScheduled() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = new Date();
        String today = sdf.format(dt);
        today = today.substring(today.length()-2,today.length());
        if (today.equals("01")) {
		long time = DateUtil.getTodayTimeDigitalSumVariation("00:00:00");
		long dealyTime = 0;
		if (time > 0) {
			dealyTime = time;
		} else {
			dealyTime = DateUtil.getNextdayTimeDigitalSumVariation("00:00:00");
		}

		executor.scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				SignReset();
			}

		}, dealyTime, DateUtil.DAY_MILLISECONDS, TimeUnit.MILLISECONDS);
	}
	}
    private void SignReset() {
    	User user = new User();
    	user.setContinuousSignDay(0);
    	user.setContinuousSignTime("");
    	UserExample userExample = new UserExample();
    	userExample.createCriteria().andIdIsNotNull();
    	try(SqlSession session = SessionUtil.getSession()) {
			UserMapper mapper = session.getMapper(UserMapper.class);
			mapper.updateByExampleSelective(user, userExample);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
